<?php
/**
 * 时间处理扩展函数
 */
class Util_Timer
{
    /**
     * 获取当前毫秒级时间戳
     */
    public static function getMicrotime()
    {
        return (int) round(microtime(true) * 1000);
    }

    /**
     * 毫秒时间戳转日期格式，精确到毫秒，x 表示毫秒
     *
     * @param integer $time 毫秒级时间戳
     * @param string $format 要格式化的时间格式
     * @return string
     */
    public static function formatMicrotime($time, $format = null)
    {
        $msec = substr($time, -3);
        $format = $format ? $format : 'Y-m-d H:i:s.x';
        $date = date($format, substr($time, 0, 10));
        return str_replace('x', $msec, $date);
    }

    /**
     * 格式化时间，格式：00:00:00
     *
     * @param integer $time 要处理的时间，单位：秒
     * @return string
     */
    public static function timeFormat($time)
    {
        if ($time < 10) {
            $time = '00:00:0' . $time;
        } elseif ($time < 60) {
            $time = '00:00:' . $time;
        } elseif ($time >= 60 && $time < 3600) {
            $m = floor($time / 60);
            $m = $m >= 10 ? $m : '0' . $m;
            $s = $time % 60;
            $s = $s >= 10 ? $s : '0' . $s;
            $time = '00:' . $m . ':' . $s;
        } elseif ($time >= 3600 && $time < 86400) {
            $h = floor($time / 3600);
            $h = $h >= 10 ? $h : '0' . $h;
            $diff = $time - $h * 3600;
            $m = floor($diff / 60);
            $m = $m >= 10 ? $m : '0' . $m;
            $s = $diff % 60;
            $s = $s >= 10 ? $s : '0' . $s;
            $time = $h . ':' . $m . ':' . $s;
        } else {
            $d = floor($time / (3600 * 24));
            $d_str = $d > 1 ? 'days ' : 'day ';
            $diff = $time - $d * 3600 * 24;
            $h = floor($diff / 3600);
            $h = $h >= 10 ? $h : '0' . $h;
            $diff = $diff - $h * 3600;
            $m = floor($diff / 60);
            $m = $m >= 10 ? $m : '0' . $m;
            $s = $diff % 60;
            $s = $s >= 10 ? $s : '0' . $s;
            $time = $d . $d_str . $h . ':' . $m . ':' . $s;
        }
        return $time;
    }

    /**
     * 格式化时间，格式：00:00
     *
     * @param integer $time 要处理的时间，单位：分
     * @return string
     */
    public static function timeFormatMinute($time)
    {
        if ($time < 10) {
            $time = '00:0' . $time;
        } elseif ($time < 60) {
            $time = '00:' . $time;
        } elseif ($time >= 60 && $time < 1440) {
            $h = floor($time / 60);
            $h = $h >= 10 ? $h : '0' . $h;
            $m = $time % 60;
            $m = $m >= 10 ? $m : '0' . $m;
            $time = $h . ':' . $m;
        } else {
            $d = floor($time / 1440);
            $d_str = $d > 1 ? 'days ' : 'day ';
            $diff = $time - $d * 1440;
            $h = floor($diff / 60);
            $h = $h >= 10 ? $h : '0' . $h;
            $diff = $diff - $h * 60;
            $m = $diff >= 10 ? $diff : '0' . $diff;
            $time = $d . $d_str . $h . ':' . $m;
        }
        return $time;
    }

    /**
     * 获取页面顶部的格式化时间
     */
    public static function getTopTime()
    {
        $timenow = time();

        /* 处理周 */
        $week_template = array(
            1 => '一',
            2 => '二',
            3 => '三',
            4 => '四',
            5 => '五',
            6 => '六',
            7 => '日'
        );
        $week = date('N', $timenow);

        /* 处理上下午 */
        $apm_template = array(
            'am' => '上午',
            'pm' => '下午'
        );
        $apm = date('a', $timenow);

        /* 返回 */
        $time_info = array(
            'week' => '星期' . $week_template[$week], // 周
            'apm' => $apm_template[$apm], // 上下午
            'time' => date('H:i:s', $timenow), // 时间
            'date' => date('Y年m月d日', $timenow) // 日期
        );
        return $time_info;
    }
}

// End ^ LF ^ encoding
